<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IList<Diagnosis>>" %>
<%@ Import Namespace="RMH.Entities"%>

<div id="diagnosisList">
</div>

<script type="text/javascript">

rmh.addDiagnosis = function(id, name) {
    if($('input.di[value=' + id + ']').length) {
        rmh.showMessage("Diagnosis already exists.");
        return;
    }
    var div = $("#diagnosisList");
    var htmlToAppend = '<div class="listItem"><input class="di" name="diagnosis" type="hidden" value="' + id + '"/>';
    htmlToAppend  += name;
    htmlToAppend += ' <span class="removeParentDiv">(remove)</span></div>';
    
    div.append(htmlToAppend);
    
};

rmh.removeParentDiv = function() {   
    $(this).closest('div').remove();
};

$(function() {
<%foreach (var diagnosis in Model){%>
      rmh.addDiagnosis(<%=diagnosis.Id %>, '<%=diagnosis.Name %>');      
<%}%>
    
    $(".removeParentDiv").live("click", rmh.removeParentDiv);
    $("#addDiagnosis").click(function(){
        var option = $('#diagnosisList :option[selected]');
        rmh.addDiagnosis(option.val(), option.text());
        return false;      
    });        
});
</script>
